const {
  $util,
} = getApp();
Page({
  data: {
    selectedDate: '',
    imgs: [],
    longPath: [],
    shortPath: [],
    title: '',
    content: '',
    conLen: 0,
    disabled: true,
    imgUrl: $util.getImgUrl(),
    checked: false,
  },
  onLoad(opts) {
    this.setData({
      groupid: opts.groupid,
    });
  },
  setRemark(e) {
    this.setData({
      title: e.detail.value,
      disabled: (e.detail.value.trim() === '' || this.data.conLen <= 0) ? true : false,
    });
  },
  setContent(e) {
    this.setData({
      content: e.detail.value,
      conLen: e.detail.value.length,
      disabled: (e.detail.value.length > 0 && this.data.title.trim() !== '') ? false : true,
    });
  },
  /**
   * 获取最新的 待提交的图片列表
   * @param {object} e 来自组件的数据
   * @param {string[]} e.detail
   * @return
   */
  imgsOnChange(e) {
    this.setData({
      imgs: e.detail,
    });
  },
  raduiOnChange(e) {
    this.setData({
      checked: !this.data.checked,
    });
  },
  /**
   * 提交
   */
  submit() {
    const {
      title,
      imgs,
      content,
      checked,
      groupid,
    } = this.data;
    let flag = checked ? '1' : '2';
    if (title.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写建议标题',
        showCancel: false,
      });
      return;
    }
    if (content.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写建议叙述',
        showCancel: false,
      });
      return;
    }
    $util.request({
      url: 'entry/wxapp/partyman.home.setadvicedetails',
      data: {
        title,
        content,
        groupid,
        type: flag,
        img: imgs.toString(),
      },
      success: (res) => {
        wx.showToast({
          title: '提交成功',
          mask: true,
          icon: 'success',
          duration: 1500,
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      },
    });
  },
});
